#! /usr/local/bin/bash

bail()
{
	echo "Bailing out ..."
	exit 1
}

usage()
{
	echo "makeDeformSum <patient dir>"
	exit 1
}

wrong()
{
	echo "Something went wrong:"
	if [ "x$1" != "x" ]; then
		echo "$1"
	fi
}

cpwd=`pwd`

#/*`````````````````````````````````````````````````\
#|  Checks on the directory                         |
#\.................................................*/

if [ "x$1" = "x" ]; then
    wrong "empty input directory"
	usage
fi

if [ ! -e "$1" ]; then
	wrong "input directory does not exist"
fi

cd "$1"

#
# Make deformed dose sum on CT1 CT3 CT4.
#

#/*`````````````````````````````````````````````````\
#|  Configuration.                                  |
#\.................................................*/
# First plan
d1='Phase_I/manual'
p1='IMRT1_CT1'
s1='sum_fx_270cGy'
n1='10'
i1='CTs/plan_im_prim/shifted'

# Second plan
d2='Phase_II/deformed'
p2='IMRT2_CT2'
s2='sum_fx_216cGy'
n2='10'
i2='CTs/plan_im_prim/shifted'

# Third plan
d3='Phase_III/deformed'
p3='IMRT3_CT3'
s3='sum_fx_216cGy'
n3='10'
i3='CTs/plan_im_prim/shifted'

# Fourth plan
d4='Phase_IV/deformed'
p4='IMRT'
#s4='sum_fx'
#n4='0'
i4='CTs/plan_im_prim'

# Output plans
odir1="$d1/IMRT_sum_CT1"
odir3="$d3/IMRT_sum_CT3"
odir4="$d4/IMRT_sum_CT4"

# Input vectormaps provided by an external engine.
#v1to2="$d2/abas/ct1_2_ct2_deformations/adaptive_ct/CT1_Elb_RTS"
#v2to3="$d3/abas/ct2_2_ct3_deformations/adaptive_ct/CT2_Elb_RTS"
#v3to4="$d4/abas/ct3_2_ct4_deformations/adaptive_ct/CT3_ELB_RTS"

# Output vectormaps directory
dv="Vectormaps"

# Input vectormaps provided by an external engine.
v1to2="$dv/CT1toCT2"
v2to3="$dv/CT2toCT3"
v3to4="$dv/CT3toCT4"

#/*`````````````````````````````````````````````````\
#|  Checks on the patient directory structure       |
#\.................................................*/

if [ ! -e "$d1" ]; then
	wrong "$d1 directory does not exist"
fi

if [ ! -e "$d1/$p1" ]; then
	wrong "$d1/$p1 directory does not exist"
fi

if [ ! -e "$d1/$p1/$s1" ]; then
	wrong "$d1/$p1/$s1 : sum file does not exist"
fi

if [ ! -e "$d1/$i1" ]; then
	wrong "$d1/$i1 : imageset does not exist"
fi

if [ ! -e "$d2" ]; then
	wrong "$d2 directory does not exist"
fi

if [ ! -e "$d2/$p2" ]; then
	wrong "$d2/$p2 directory does not exist"
fi

if [ ! -e "$d2/$p2/$s2" ]; then
	wrong "$d2/$p2/$s2 : sum file does not exist"
fi

if [ ! -e "$d2/$i2" ]; then
	wrong "$d2/$i2 : imageset does not exist"
fi

if [ ! -e "$d3" ]; then
	wrong "$d3 directory does not exist"
fi

if [ ! -e "$d3/$p3" ]; then
	wrong "$d3/$p3 directory does not exist"
fi

if [ ! -e "$d3/$p3/$s3" ]; then
	wrong "$d3/$p3/$s3 : sum file does not exist"
fi

if [ ! -e "$d3/$i3" ]; then
	wrong "$d3/$i3 : imageset does not exist"
fi

#/*`````````````````````````````````````````````````\
#|  Checks on the vectorfields                      |
#\.................................................*/

if [ ! -e "$v1to2.mhd" ]; then
	wrong "$v1to2 mhd file does not exist"
fi

if [ ! -e "$v1to2.raw" ]; then
	wrong "$v1to2 raw file does not exist"
fi

if [ ! -e "$v2to3.mhd" ]; then
	wrong "$v2to3 mhd file does not exist"
fi

if [ ! -e "$v2to3.raw" ]; then
	wrong "$v2to3 raw file does not exist"
fi

if [ ! -e "$v3to4.mhd" ]; then
	wrong "$v3to4 mhd file does not exist"
fi

if [ ! -e "$v3to4.raw" ]; then
	wrong "$v3to4 raw file does not exist"
fi

#/*`````````````````````````````````````````````````\
#|  Generate vectormaps.                            |
#|  Input vectormaps: 1->2 and 2->3.                |
#\.................................................*/

# Generate vectormaps for dose on CT3.

# 3->2 from 2->3
echo "inversion 2-3 --- 3->2 starting"
v3to2="$dv/CT3toCT2"
createMhd "$v3to2" "$d3/$i3"
invertVectormap "$v3to2" "$v2to3"
echo "inversion 2->3 --- 3->2 done"

read answer

# 3->1, using inversions from 1->2 and 2->3.
echo "inversion 1-2 --- 2->1 starting"
v2to1="$dv/CT2toCT1"
echo "v1to2 = $v1to2"
echo "v2to1 = $v2to1"
cp -p "$v2to3.mhd" "$v2to1.mhd"
invertVectormap "$v2to1" "$v1to2"
echo "inversion 1->2 --- 2->1 done"

read answer

v3to1="$dv/CT3toCT1"
cp -p "$v3to2.mhd" "$v3to1.mhd"
catVectormap "$v3to1" "$v3to2" "$v2to1"
echo "concat to 3->1 done"

# Generate additional vectormaps for dose on CT1.

# 1->3 using concatenation of 1->2 and 2->3.
v1to3="$dv/CT1toCT3"
cp -p "$v1to2.mhd" "$v1to3.mhd"
catVectormap "$v1to3" "$v1to2" "$v2to3"

# Generate vectormaps for dose on CT4

# 4->3 using inversions from 3->4
v4to3="$dv/CT4toCT3"
echo "createMhd $v4to3 $d4/$i4"
createMhd "$v4to3" "$d4/$i4"
echo "invertVectormap $v4to3 $v3to4"
invertVectormap "$v4to3" "$v3to4"
echo "inversion 3->4 --- 4->3 done"

# 4->2 using concatenation of 4->3 and 3->2
v4to2="$dv/CT4toCT2"
cp -p "$v4to3.mhd" "$v4to2.mhd"
catVectormap "$v4to2" "$v4to3" "$v3to2"
echo "concat to 4->2 done"

# 4->1 using concatenation of 4->3 and 3->1
v4to1="$dv/CT4toCT1"
cp -p "$v4to3.mhd" "$v4to1.mhd" 
catVectormap "$v4to1" "$v4to3" "$v3to1"
echo "concat to 4->1 done"

#/*`````````````````````````````````````````````````\
#|  Sum on CT4.                                     |
#\.................................................*/

# Make directory.
mkdir "$odir4"
cd "$odir4" || exit 1
ln -s "../a"
ln -s "../patient_name"
ln -s "../plan_im"
mkdir b v g
cd "$cpwd" || exit 1

# Fourth dose grid: (can copy, or) use boxana
#cp -p "$d4/$p4/grid" "$odir4/grid"
grid_around_ana "$odir4/grid" "$d4/a/skin" -rx 0.4 -ry 0.4 -rz 0.4
echo "grid around skin created"

# Third dose grid: needs transform 4->3, using inversion from 3->4
cp -p "$odir4/grid" "$odir4/g/ac"
pwd
deformDose "$odir4/g/ac" "$v4to3" "$d3/$p3/$s3" \
    -ct1 "$d3/$i3" -pi1 "$d3/plan_im" \
    -ct2 "$d4/$i4" -pi2 "$d4/plan_im"
echo "deformDose 3->4 done"

# Second dose grid: need transform 4->2, using inversion from 2->3 and 3->4
cp -p "$odir4/grid" "$odir4/g/ab"
deformDose "$odir4/g/ab" "$v4to2" "$d2/$p2/$s2" \
  -ct1 "$d2/$i2" -pi1 "$d2/plan_im" \
  -ct2 "$d4/$i4" -pi2 "$d4/plan_im"
echo "deformDose 2->4 done"

# First dose grid: need transform 4->1, using inversions from 1->2, 2->3 and 3->4
cp -p "$odir4/grid" "$odir4/g/aa"
deformDose "$odir4/g/aa" "$v4to1" "$d1/$p1/$s1" \
  -ct1 "$d1/$i1" -pi1 "$d1/plan_im" \
  -ct2 "$d4/$i4" -pi2 "$d4/plan_im"
echo "deformDose 1->4 done"

#
# Combine
#
canbeam "$odir4/b/aa" "$d4/a/skin" 25062 "$d4/a/skin" 10 10
cp -p "$odir4/b/aa" "$odir4/b/ab"
cp -p "$odir4/b/aa" "$odir4/b/ac"
list=".xplandisp_vis .xvsim_vis logfile"
for i in $list; do
  if [ -f "$d3/$p3/$i" ]; then
    cp -p "$d3/$p3/$i" "$odir4/$i"
  fi
done

cd "$odir4" || exit 1

echo "$n1" "$n2" "$n3" | gawk '//{
  n=split($0,a); for(i=1;i<=n;++i) printf " %g",0.01*a[i]; }' | asc2wt "wt"
cp -p "wt" "wt_total"
cp -p "wt" "wt_opt"

construct_points
vdose

touch v/*
sleep 1
touch g/*
cd "$cpwd" || exit 1


#/*`````````````````````````````````````````````````\
#|  Sum on CT3.                                     |
#\.................................................*/

# Make directory.
mkdir "$odir3"
cd "$odir3" || exit 1
ln -s "../a"
ln -s "../patient_name"
ln -s "../plan_im"
mkdir b v g
cd "$cpwd" || exit 1

# Third dose grid: can copy
cp -p "$d3/$p3/grid" "$odir3/grid"
cp -p "$d3/$p3/$s3" "$odir3/g/ac"

# Second dose grid: need transform 3->2, using inversion from 2->3
cp -p "$d3/$p3/grid" "$odir3/g/ab"
deformDose "$odir3/g/ab" "$v3to2" "$d2/$p2/$s2" \
  -ct1 "$d2/$i2" -pi1 "$d2/plan_im" \
  -ct2 "$d3/$i3" -pi2 "$d3/plan_im"

# First dose grid: need transform 3->1, using inversions from 1->2 and 2->3.
cp -p "$d3/$p3/grid" "$odir3/g/aa"
deformDose "$odir3/g/aa" "$v3to1" "$d1/$p1/$s1" \
  -ct1 "$d1/$i1" -pi1 "$d1/plan_im" \
  -ct2 "$d3/$i3" -pi2 "$d3/plan_im"

#
# Combine
#
cp -p "$d3/$p3/b/aa" "$odir3/b/aa"
cp -p "$d3/$p3/b/aa" "$odir3/b/ab"
cp -p "$d3/$p3/b/aa" "$odir3/b/ac"
list=".xplandisp_vis .xvsim_vis logfile"
for i in $list; do
  if [ -f "$d3/$p3/$i" ]; then
    cp -p "$d3/$p3/$i" "$odir3/$i"
  fi
done

cd "$odir3" || exit 1

echo "$n1" "$n2" "$n3" | gawk '//{
  n=split($0,a); for(i=1;i<=n;++i) printf " %g",0.01*a[i]; }' | asc2wt "wt"
cp -p "wt" "wt_total"
cp -p "wt" "wt_opt"

construct_points
vdose

touch v/*
sleep 1
touch g/*
cd "$cpwd" || exit 1


#/*`````````````````````````````````````````````````\
#|  Sum on CT1.                                     |
#\.................................................*/

# Make directory.
mkdir "$odir1"
cd "$odir1" || exit 1
ln -s "../a"
ln -s "../patient_name"
ln -s "../plan_im"
mkdir b v g
cd "$cpwd" || exit 1

# First dose grid: can copy
cp -p "$d1/$p1/grid" "$odir1/grid"
cp -p "$d1/$p1/$s1" "$odir1/g/aa"


# Second dose grid: need transform 1->2.
cp -p "$d1/$p1/grid" "$odir1/g/ab"
deformDose "$odir1/g/ab" "$v1to2" "$d2/$p2/$s2" \
  -ct1 "$d2/$i2" -pi1 "$d2/plan_im" \
  -ct2 "$d1/$i1" -pi2 "$d1/plan_im"


# Third dose grid: need transform 1->3, using concatenation of 1->2 and 2->3.
cp -p "$d1/$p1/grid" "$odir1/g/ac"
deformDose "$odir1/g/ac" "$v1to3" "$d3/$p3/$s3" \
  -ct1 "$d3/$i3" -pi1 "$d3/plan_im" \
  -ct2 "$d1/$i1" -pi2 "$d1/plan_im"

#
# Combine
#
cp -p "$d1/$p1/b/aa" "$odir1/b/aa"
cp -p "$d1/$p1/b/aa" "$odir1/b/ab"
cp -p "$d1/$p1/b/aa" "$odir1/b/ac"
list=".xplandisp_vis .xvsim_vis logfile"
for i in $list; do
  if [ -f "$d1/$p1/$i" ]; then
    cp -p "$d1/$p1/$i" "$odir1/$i"
  fi
done

cd "$odir1" || exit 1

echo "$n1" "$n2" "$n3" | gawk '//{
  n=split($0,a); for(i=1;i<=n;++i) printf " %g",0.01*a[i]; }' | asc2wt "wt"
cp -p "wt" "wt_total"
cp -p "wt" "wt_opt"

construct_points
sleep 1
vdose

sleep 1
touch v/*
sleep 1
touch g/*

# Sum all grid files to one sum file
gList=""
for i in `ls g`; do
    gList="$gList -g g/$i"
done
echo "Sum grid files from g directory to sum file"
echo "sum_grids_sep -w wt -o sum ${gList}"
sum_grids_sep -w wt -o sum ${gList}

cd "$cpwd" || exit 1

cd "$cpwd"
